//
//  ObersveArray.m
//  KVC的实现
//
//  Created by webplus on 17/8/31.
//  Copyright © 2017年 sanyi. All rights reserved.
//

#import "ObersveArray.h"

@implementation ObersveArray
-(id)init{
    if (self == [super init]){
        _arr = [NSMutableArray new];
        NSLog(@"%p",_arr);
        [self addObserver:self forKeyPath:@"arr" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    }
    return self;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    NSLog(@"%@",change);
}
-(void)dealloc{
    [self removeObserver:self forKeyPath:@"arr"]; //一定要在dealloc里面移除观察
}
-(void)addItem{
    [_arr addObject:@"1"];
    NSLog(@"%p",_arr);
}
-(void)addItemObserver{
    [[self mutableArrayValueForKey:@"arr"] addObject:@"1"];
    NSLog(@"%p",_arr);
}
-(void)removeItemObserver{
    [[self mutableArrayValueForKey:@"arr"] removeLastObject];
    NSLog(@"%p",_arr);
}


@end
